from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# chromedriver路径
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    wait.until(EC.presence_of_element_located((By.ID, "username"))).send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()
    wait.until(EC.presence_of_element_located((By.ID, "mainmenutd_products")))

# 进入新建仓库页面
def go_to_warehouse_creation():
    browser.find_element(By.ID, "mainmenutd_products").click()
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.menu_contenu_product_stock_card .vsmenu'))).click()

# 新建仓库函数
def add_warehouse(data):
    wait.until(EC.presence_of_element_located((By.NAME, "libelle"))).send_keys(data["code"])
    browser.find_element(By.NAME, "lieu").send_keys(data["location"])
    browser.find_element(By.ID, "desc").send_keys(data["description"])
    browser.find_element(By.NAME, "address").send_keys(data["address"])
    browser.find_element(By.ID, "zipcode").send_keys(data["zipcode"])
    browser.find_element(By.ID, "town").send_keys(data["city"])
    #此参数全填9
    browser.find_element(
        By.CSS_SELECTOR,
        f"select#selectcountry_id > option[value='{data['country_value']}']"
    ).click()
    browser.find_element(By.NAME, "phone").send_keys(data["phone"])
    browser.find_element(By.NAME, "fax").send_keys(data["fax"])
    #此参数1为打开，2为关闭，关闭填一个就好
    browser.find_element(
        By.CSS_SELECTOR,
        f"select#warehousestatus > option[value='{data['status_value']}']"
    ).click()
    browser.find_element(By.NAME, "add").click()
    sleep(2)  # 等待页面处理，后期可用断言或 URL 检查优化

# 主函数
if __name__ == '__main__':
    try:
        login()
        # 仓库数据测试用例（3正向+3反向，共6个）
        #测试结果正向全部通过，反向第一个提示编号为空，第二个添加成功，第三个直接提示找不到元素
        warehouse_list = [
            # --------------------- 正向测试用例 ---------------------
            {
                "code": "001",
                "location": "仓库1",
                "description": "用于存储衣服，方便调取",
                "address": "XX路123号",
                "zipcode": "200001",
                "city": "上海",
                "country_value": 9,  # 中国
                "phone": "021-12345678",
                "fax": "021-87654321",
                "status_value": 1  # 开启状态
            },
            {
                "code": "002",
                "location": "保税仓库",
                "description": "海关监管保税仓库，支持跨境商品暂存",
                "address": "上海市浦东新区保税大道88号",
                "zipcode": "200120",
                "city": "上海",
                "country_value": 9,  # 中国
                "phone": "021-56789012",
                "fax": "021-56789013",
                "status_value": 1
            },
            {
                "code": "003",
                "location": "哥本哈根仓库",
                "description": "服务欧洲市场的跨境中转仓",
                "address": "丹麦哥本哈根市物流园区12号",
                "zipcode": "1000",
                "city": "哥本哈根",
                "country_value": 80,  # 丹麦
                "phone": "+45-12345678",
                "fax": "+45-87654321",
                "status_value": 1
            },

            # --------------------- 反向测试用例 ---------------------
            {
                "code": "",  # 空编码（必填项）
                "location": "测试仓库",
                "description": "反向测试用例",
                "address": "测试地址",
                "zipcode": "100000",
                "city": "北京",
                "country_value": 9,
                "phone": "010-12345678",
                "fax": "010-87654321",
                "status_value": 1,
                "expected_result": "添加失败，提示\"编码不能为空\""
            },
            {
                "code": "a" * 31,  # 超过30位的编码
                "location": "超长编码仓库",
                "description": "测试系统对超长字段的处理",
                "address": "测试地址",
                "zipcode": "100000",
                "city": "北京",
                "country_value": 9,
                "phone": "010-12345678",
                "fax": "010-87654321",
                "status_value": 1,
                "expected_result": "添加失败，提示\"编码长度不能超过30位\""
            },
            {
                "code": "004",
                "location": "无效状态仓库",
                "description": "测试非法状态值",
                "address": "测试地址",
                "zipcode": "100000",
                "city": "北京",
                "country_value": 9,
                "phone": "010-12345678",
                "fax": "010-87654321",
                "status_value": 3,  # 非法状态值（仅允许1/2）
                "expected_result": "添加失败，提示\"状态值无效\""
            }
        ]
        for data in warehouse_list:
            go_to_warehouse_creation()
            add_warehouse(data)

    except Exception as e:
        print("出错了：", type(e).__name__, str(e))
    finally:
        sleep(1)
        browser.quit()
